package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.event.EventMain;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class EventBaseData {
    private static Logger logger = LoggerFactory.getLogger(EventBaseData.class);

    private static ConcurrentHashMap<Integer, EventMain> eventMainMap = new ConcurrentHashMap<>(); // 事件主表
    private static ConcurrentHashMap<Integer, List<EventMain>> eventMainTriggerMap = new ConcurrentHashMap<>(); // 根据触发类型分类的事件

    public static void initBaseData(List<Future> futures) {
        futures.add(initEventMain());
    }

    /**
     * 初始化事件主表
     */
    public static Future<Void> initEventMain() {
        String table = "event_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, EventMain> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<EventMain>> categoryTmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        EventMain entity = new EventMain(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        categoryTmp.computeIfAbsent(entity.getEventGroup(), k -> new ArrayList<>()).add(entity);
                    }
                    eventMainMap = tmp;
                    eventMainTriggerMap = categoryTmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static EventMain getEventMain(int id) {
        EventMain result = eventMainMap.get(id);
        if (result == null) {
            logger.error("getEventMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, EventMain> getEventMainMap() { return eventMainMap; }

    public static ConcurrentHashMap<Integer, List<EventMain>> getEventMainTriggerMap() {
        return eventMainTriggerMap;
    }

    public static List<EventMain> getEventMainList(int triggerType) {
        List<EventMain> result = eventMainTriggerMap.get(triggerType);
        if (result == null) {
            logger.error("getEventMainList error key = " + triggerType);
        }
        return result;
    }

}
